package cn.yingxd.demo.javatraining.tester;

import reactor.core.publisher.Mono;

import java.time.Duration;

public class mono {
    public static void test() {
// 1. Create Mono
        Mono<String> staticMono = Mono.just("Hello");
        Mono<String> emptyMono = Mono.empty();
        Mono<String> errorMono = Mono.error(new RuntimeException("Oops"));

        // 2. Transform values
        staticMono
                .map(String::toUpperCase)
                .flatMap(s -> Mono.delay(Duration.ofSeconds(1)).thenReturn(s + " World"))
                .subscribe(System.out::println);

        // 3. Handle errors
        errorMono
                .onErrorResume(e -> Mono.just("Fallback Value"))
                .subscribe(System.out::println);

        // 4. Combine Monos
        Mono.zip(
                Mono.just("Apple"),
                Mono.delay(Duration.ofMillis(500)).thenReturn(42)
            )
            .subscribe(tuple -> System.out.println(tuple.getT1() + " - " + tuple.getT2()));

        // 5. Block for result (testing/demo only)
        String result = staticMono.block();
        System.out.println("Blocked result: " + result);

        // Keep main thread alive to see async results
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
